﻿using Common.Contract;
using Common.Contract.Authentication.Record;
using Common.Contract.CrossSelling;
using Common.Contract.CrossSelling.Record;
using Common.Contract.CrossSelling.Request;
using Common.Contract.CrossSelling.Response;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Data.Repository.Enum;
using Common.Service;
using Common.Service.DataTables;
using SMECustomerWebsite.Core.Configuration;
using SMECustomerWebsite.Filters;
using SMECustomerWebsite.Models.ViewModels.CrossSelling;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;

namespace SMECustomerWebsite.Controllers
{
    public class XsellingController : ControllerBase
    {
        #region Oveview
        //
        // GET: /Xselling/
        [CustomActionFilter]
        [Authorize]
        public ActionResult BaseOnHoldingProduct(string product)
        {
            UserRecord resultCustomer = new UserRecord();
            JsonResult listProduct = new JsonResult();
            var customerRecords = new RetrieveListProductResponse();
            using (ServiceClient<ICrossSelling> serviceClient = new ServiceClient<ICrossSelling>())
            {
                RetrieveListProductRequest request = new RetrieveListProductRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.XsellingController.Index"
                    },
                    Page = new PageRecord
                    {
                        PageSize = 10000,
                        PageIndex = 0
                    },
                    Lang = this.CurrentLang
                };
                customerRecords = serviceClient.Proxy.RetrieveListProduct(request);
                var objectItem = customerRecords.Records.Select(x => new
                {
                    id = x.Id,
                    text = x.ProductName,
                });
                listProduct = Json(objectItem);
            }
            Dictionary<string, XSellingProductRecord> listProducts = customerRecords.Records.ToDictionary(x => x.Id.ToString());
            CrossSellingViewModel vm = new CrossSellingViewModel
            {
                CurrentUser = this.CurrentUserRecord,
                ListProduct = listProduct,
                productName = DataProductXSelling.MapProductData(string.IsNullOrEmpty(product) ? string.Empty : product, this.CurrentLang),
                productId = string.IsNullOrEmpty(product) ? "unknow" : product,
                MaxLift = !string.IsNullOrEmpty(product) && listProducts.ContainsKey(product) ? listProducts[product].MaxLift : "10",
                MinLift = !string.IsNullOrEmpty(product) && listProducts.ContainsKey(product) ? listProducts[product].MinLift : "1",
            };
            return View(vm);
        }



        [Authorize]
        [HttpPost]
        public ActionResult CustomerXSellingData(DataTablesPageRequest pageRequest, string product)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICrossSelling> serviceClient = new ServiceClient<ICrossSelling>())
            {
                FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        filter.CusOpenDate = pageRequest.SearchColumns[7];
                        filter.Lift = pageRequest.SearchColumns[8];
                        break;
                    case "2":
                        filter.CusOpenDate = pageRequest.SearchColumns[6];
                        filter.Lift = pageRequest.SearchColumns[7];
                        break;
                    case "3":
                        filter.CusOpenDate = pageRequest.SearchColumns[5];
                        filter.Lift = pageRequest.SearchColumns[6];
                        break;
                    case "4":
                        filter.CusOpenDate = pageRequest.SearchColumns[3];
                        filter.Lift = pageRequest.SearchColumns[4];
                        break;
                    default:
                        break;
                }
                RetrieveCustomerByProductRequest request = new RetrieveCustomerByProductRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.XsellingController.CustomerXSellingData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Lang = CurrentLang,
                    CurrentUser = CurrentUserRecord,
                    Filter = filter,
                    Product = string.IsNullOrEmpty(product) ? "unknow" : product
                };
                var customerRecords = new RetrieveCustomerByProductResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerByProduct(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<XSellingCustomerRecord>() : customerRecords.Records;
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "4":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.LiftIndex,
                        }).ToList()
                        };
                        break;
                    case "3":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.LiftIndex,
                        }).ToList()
                        };
                        break;
                    case "2":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Branch,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.LiftIndex,
                        }).ToList()
                        };
                        break;
                    case "1":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Zone,
                                                             x.Branch,
                                                             string.IsNullOrEmpty(x.Dao) ? string.Empty:x.Dao,
                                                             string.IsNullOrEmpty(x.DaoName)? string.Empty: x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.LiftIndex,
                        }).ToList()
                        };
                        break;
                    default:
                        break;
                }
            }
            return Json(pageResponse);
        }

        #endregion
        #region Customer used product
        [CustomActionFilter]
        [Authorize]
        public ActionResult HoldProduct(string product)
        {
            UserRecord resultCustomer = new UserRecord();
            JsonResult listProduct = new JsonResult();
            using (ServiceClient<ICrossSelling> serviceClient = new ServiceClient<ICrossSelling>())
            {
                RetrieveListProductRequest request = new RetrieveListProductRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.XsellingController.Index"
                    },
                    Page = new PageRecord
                    {
                        PageSize = 10000,
                        PageIndex = 0
                    },
                    Lang = CurrentLang
                };
                var customerRecords = new RetrieveListProductResponse();
                customerRecords = serviceClient.Proxy.RetrieveListHoldProduct(request);
                var objectItem = customerRecords.Records.Select(x => new
                {
                    id = x.Product,
                    text = x.ProductName,
                });
                listProduct = Json(objectItem);
            }
            CrossSellingViewModel vm = new CrossSellingViewModel
            {
                CurrentUser = this.CurrentUserRecord,
                ListProduct = listProduct,
                productName = !string.IsNullOrEmpty(product) ? product : "unknow",
                productId = product
            };
            return View(vm);
        }

        [Authorize]
        [HttpPost]
        public ActionResult CustomerHoldProductData(DataTablesPageRequest pageRequest, string product)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICrossSelling> serviceClient = new ServiceClient<ICrossSelling>())
            {
                FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        filter.CusOpenDate = pageRequest.SearchColumns[7];
                        filter.Lift = pageRequest.SearchColumns[8];
                        break;
                    case "2":
                        filter.CusOpenDate = pageRequest.SearchColumns[6];
                        filter.Lift = pageRequest.SearchColumns[7];
                        break;
                    case "3":
                        filter.CusOpenDate = pageRequest.SearchColumns[5];
                        filter.Lift = pageRequest.SearchColumns[6];
                        break;
                    case "4":
                        filter.CusOpenDate = pageRequest.SearchColumns[3];
                        filter.Lift = pageRequest.SearchColumns[4];
                        break;
                    default:
                        break;
                }
                RetrieveCustomerHoldProductRequest request = new RetrieveCustomerHoldProductRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.XsellingController.CustomerXSellingData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Lang = CurrentLang,
                    CurrentUser = CurrentUserRecord,
                    Filter = filter,
                    Product = product
                };
                var customerRecords = new RetrieveCustomerHoldProductResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerHoldProduct(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<XSellingCustomerRecord>() : customerRecords.Records;
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "4":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.LastTimeUse
                        }).ToList()
                        };
                        break;
                    case "3":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.LastTimeUse
                        }).ToList()
                        };
                        break;
                    case "2":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Branch,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.LastTimeUse
                        }).ToList()
                        };
                        break;
                    case "1":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Zone,
                                                             x.Branch,
                                                             string.IsNullOrEmpty(x.Dao) ? string.Empty:x.Dao,
                                                             string.IsNullOrEmpty(x.DaoName)? string.Empty: x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.LastTimeUse
                        }).ToList()
                        };
                        break;
                    default:
                        break;
                }
            }
            return Json(pageResponse);
        }

        #endregion
        #region Pre approved program

        [CustomActionFilter]
        [Authorize]
        public ActionResult PreApprovedCredit()
        {
            CrossSellingViewModel vm = new CrossSellingViewModel
            {
                CurrentUser = this.CurrentUserRecord
            };
            return View(vm);
        }
        [CustomActionFilter]
        [Authorize]
        public ActionResult PreApprovedCredit87()
        {
            CrossSellingViewModel vm = new CrossSellingViewModel
            {
                CurrentUser = this.CurrentUserRecord
            };
            return View(vm);
        }
        [CustomActionFilter]
        [Authorize]
        public ActionResult PreApprovedCredit21()
        {
            CrossSellingViewModel vm = new CrossSellingViewModel
            {
                CurrentUser = this.CurrentUserRecord
            };
            return View(vm);
        }
        [CustomActionFilter]
        [Authorize]
        public ActionResult PreApprovedCredit21LackFR()
        {
            CrossSellingViewModel vm = new CrossSellingViewModel
            {
                CurrentUser = this.CurrentUserRecord
            };
            return View(vm);
        }
        [CustomActionFilter]
        [Authorize]
        public ActionResult PreApprovedCredit21NotEligible()
        {
            CrossSellingViewModel vm = new CrossSellingViewModel
            {
                CurrentUser = this.CurrentUserRecord
            };
            return View(vm);
        }

        [CustomActionFilter]
        [Authorize]
        public ActionResult PreApprovedBil()
        {
            CrossSellingViewModel vm = new CrossSellingViewModel
            {
                CurrentUser = this.CurrentUserRecord
            };
            return View(vm);
        }

        [CustomActionFilter]
        [Authorize]
        public ActionResult PreApprovedWC()
        {
            CrossSellingViewModel vm = new CrossSellingViewModel
            {
                CurrentUser = this.CurrentUserRecord
            };
            return View(vm);
        }

        [Authorize]
        [HttpPost]
        public ActionResult PreApprovedCreditData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICrossSelling> serviceClient = new ServiceClient<ICrossSelling>())
            {
                FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        filter.CusOpenDate = pageRequest.SearchColumns[7];
                        filter.Balance = pageRequest.SearchColumns[8];
                        break;
                    case "2":
                        filter.CusOpenDate = pageRequest.SearchColumns[6];
                        filter.Balance = pageRequest.SearchColumns[7];
                        break;
                    case "3":
                        filter.CusOpenDate = pageRequest.SearchColumns[5];
                        filter.Balance = pageRequest.SearchColumns[6];
                        break;
                    case "4":
                        filter.CusOpenDate = pageRequest.SearchColumns[3];
                        filter.Balance = pageRequest.SearchColumns[4];
                        break;
                    default:
                        break;
                }
                RetrievePreApprovedCustCreditCardConversionRequest request = new RetrievePreApprovedCustCreditCardConversionRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.XsellingController.CustomerXSellingData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Lang = CurrentLang,
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                var customerRecords = new RetrievePreApprovedCustCreditCardConversionResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerPreApprovedCreditCardConversion(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<XSellingCustomerRecord>() : customerRecords.Records;
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "4":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.LimitCreditCard,
                                                             x.LimitTimeCreditCard.ToString()
                        }).ToList()
                        };
                        break;
                    case "3":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.LimitCreditCard,
                                                             x.LimitTimeCreditCard.ToString()
                        }).ToList()
                        };
                        break;
                    case "2":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Branch,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.LimitCreditCard,
                                                             x.LimitTimeCreditCard.ToString()
                        }).ToList()
                        };
                        break;
                    case "1":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Zone,
                                                             x.Branch,
                                                             string.IsNullOrEmpty(x.Dao) ? string.Empty:x.Dao,
                                                             string.IsNullOrEmpty(x.DaoName)? string.Empty: x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.LimitCreditCard,
                                                             x.DateAprove.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.LimitCreditCard,
                                                             x.DateAprove.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
                        }).ToList()
                        };
                        break;
                    default:
                        break;
                }
            }
            return Json(pageResponse);
        }


        [Authorize]
        [HttpPost]
        public ActionResult PreApprovedCreditData87(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICrossSelling> serviceClient = new ServiceClient<ICrossSelling>())
            {
                FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        filter.CusOpenDate = pageRequest.SearchColumns[7];
                        filter.Balance = pageRequest.SearchColumns[8];
                        break;
                    case "2":
                        filter.CusOpenDate = pageRequest.SearchColumns[6];
                        filter.Balance = pageRequest.SearchColumns[7];
                        break;
                    case "3":
                        filter.CusOpenDate = pageRequest.SearchColumns[5];
                        filter.Balance = pageRequest.SearchColumns[6];
                        break;
                    case "4":
                        filter.CusOpenDate = pageRequest.SearchColumns[3];
                        filter.Balance = pageRequest.SearchColumns[4];
                        break;
                    default:
                        break;
                }
                RetrievePreApprovedCustCreditCardConversionRequest request = new RetrievePreApprovedCustCreditCardConversionRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.XsellingController.CustomerXSellingData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Lang = CurrentLang,
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                var customerRecords = serviceClient.Proxy.RetrieveCustomerPreApprovedCreditCardConversion(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<XSellingCustomerRecord>() : customerRecords.Records;
                List<object[]> listCust = new List<object[]>();
                if (resultCustomer.Count > 0)
                {
                    switch (CurrentUserRecord.GroupIdentifier)
                    {
                        case "4":
                            listCust = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.LimitCreditCard,
                                                             x.LimitTimeCreditCard.ToString()
                        }).ToList();
                            break;
                        case "3":
                            listCust = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.LimitCreditCard,
                                                             x.LimitTimeCreditCard.ToString()
                        }).ToList();
                            break;
                        case "2":
                            listCust = resultCustomer.Select(x => new object[]{
                                                             x.Branch,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.LimitCreditCard,
                                                             x.LimitTimeCreditCard.ToString()
                        }).ToList();
                            break;
                        case "1":
                            listCust = resultCustomer.Select(x => new object[]{
                                                             x.Zone,
                                                             x.Branch,
                                                             string.IsNullOrEmpty(x.Dao) ? string.Empty:x.Dao,
                                                             string.IsNullOrEmpty(x.DaoName)? string.Empty: x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.LimitCreditCard,
                                                             x.LimitTimeCreditCard.ToString()
                        }).ToList();
                            break;
                        default:
                            break;
                    }
                }
                pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = listCust
                        };
            }
            return Json(pageResponse);
        }
        [Authorize]
        [HttpPost]
        public ActionResult PreApprovedCredit21Data(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "1":
                    filter.CusOpenDate = pageRequest.SearchColumns[7];
                    filter.Balance = pageRequest.SearchColumns[8];
                    break;
                case "2":
                    filter.CusOpenDate = pageRequest.SearchColumns[6];
                    filter.Balance = pageRequest.SearchColumns[7];
                    break;
                case "3":
                    filter.CusOpenDate = pageRequest.SearchColumns[5];
                    filter.Balance = pageRequest.SearchColumns[6];
                    break;
                case "4":
                    filter.CusOpenDate = pageRequest.SearchColumns[3];
                    filter.Balance = pageRequest.SearchColumns[4];
                    break;
                default:
                    break;
            }
            var customerRecords = RetrievePreApprovedCustomerCreditCardForLoan(pageRequest, filter, 1);
            var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<XSellingCreditCardForLoanRecord>() : customerRecords.Records;
            List<object[]> listCust = new List<object[]>();
            if (resultCustomer.Count > 0)
            {
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "4":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Cif,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.GioiHanChoVayKhongTsbd.ToString("##,##0.00"),
                                                             x.NgayPheDuyet.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             0,
                                                             5
                        }).ToList();
                        break;
                    case "3":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.Cif,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.GioiHanChoVayKhongTsbd.ToString("##,##0.00"),
                                                             x.NgayPheDuyet.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             0,
                                                             5
                        }).ToList();
                        break;
                    case "2":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Branch,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.Cif,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.GioiHanChoVayKhongTsbd.ToString("##,##0.00"),
                                                             x.NgayPheDuyet.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             0,
                                                             5
                        }).ToList();
                        break;
                    case "1":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Zone,
                                                             x.Branch,
                                                             string.IsNullOrEmpty(x.Dao) ? string.Empty:x.Dao,
                                                             string.IsNullOrEmpty(x.DaoName)? string.Empty: x.DaoName,
                                                             x.Cif,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.GioiHanChoVayKhongTsbd.ToString("##,##0.00"),
                                                             x.NgayPheDuyet.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             0,
                                                             5
                        }).ToList();
                        break;
                    default:
                        break;
                }
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = listCust
                };
            }
            else
            {
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = 1,
                    sColumns = pageRequest.ColumnNames,
                    aaData = new List<object[]>()
                };
                pageResponse.aaData.Add(new object[] { customerRecords.ErrorMessage });
            }
            return Json(pageResponse);
        }
        [Authorize]
        [HttpPost]
        public ActionResult PreApprovedCredit21LackFRData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "1":
                    filter.CusOpenDate = pageRequest.SearchColumns[7];
                    filter.Balance = pageRequest.SearchColumns[8];
                    break;
                case "2":
                    filter.CusOpenDate = pageRequest.SearchColumns[6];
                    filter.Balance = pageRequest.SearchColumns[7];
                    break;
                case "3":
                    filter.CusOpenDate = pageRequest.SearchColumns[5];
                    filter.Balance = pageRequest.SearchColumns[6];
                    break;
                case "4":
                    filter.CusOpenDate = pageRequest.SearchColumns[3];
                    filter.Balance = pageRequest.SearchColumns[4];
                    break;
                default:
                    break;
            }
            var customerRecords = RetrievePreApprovedCustomerCreditCardForLoan(pageRequest, filter, 2);
            var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<XSellingCreditCardForLoanRecord>() : customerRecords.Records;
            List<object[]> listCust = new List<object[]>();
            if (resultCustomer.Count > 0)
            {
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "4":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Cif,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.GioiHanChoVayKhongTsbd.ToString("##,##0.00"),
                                                             x.NgayPheDuyet.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             0,
                                                             5
                        }).ToList();
                        break;
                    case "3":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.Cif,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.GioiHanChoVayKhongTsbd.ToString("##,##0.00"),
                                                             x.NgayPheDuyet.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             0,
                                                             5
                        }).ToList();
                        break;
                    case "2":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Branch,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.Cif,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.GioiHanChoVayKhongTsbd.ToString("##,##0.00"),
                                                             x.NgayPheDuyet.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             0,
                                                             5
                        }).ToList();
                        break;
                    case "1":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Zone,
                                                             x.Branch,
                                                             string.IsNullOrEmpty(x.Dao) ? string.Empty:x.Dao,
                                                             string.IsNullOrEmpty(x.DaoName)? string.Empty: x.DaoName,
                                                             x.Cif,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.GioiHanChoVayKhongTsbd.ToString("##,##0.00"),
                                                             x.NgayPheDuyet.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             0,
                                                             5
                        }).ToList();
                        break;
                    default:
                        break;
                }
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = listCust
                };
            }
            else
            {
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = 1,
                    sColumns = pageRequest.ColumnNames,
                    aaData = new List<object[]>()
                };
                pageResponse.aaData.Add(new object[] { customerRecords.ErrorMessage });
            }
            return Json(pageResponse);
        }
        [Authorize]
        [HttpPost]
        public ActionResult PreApprovedCredit21NotEligibleData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
            switch (CurrentUserRecord.GroupIdentifier)
            {
                case "1":
                    filter.CheckLhdn = pageRequest.SearchColumns[7];
                    filter.CheckKhoanVayPheDuyet = pageRequest.SearchColumns[8];
                    filter.CheckThoiGianPheDuyet = pageRequest.SearchColumns[9];
                    filter.CheckKhChuaCoThe = pageRequest.SearchColumns[10];
                    filter.CheckConGioiHanChoVayKhongTsbd = pageRequest.SearchColumns[11];
                    break;
                case "2":
                    filter.CheckLhdn = pageRequest.SearchColumns[6];
                    filter.CheckKhoanVayPheDuyet = pageRequest.SearchColumns[7];
                    filter.CheckThoiGianPheDuyet = pageRequest.SearchColumns[8];
                    filter.CheckKhChuaCoThe = pageRequest.SearchColumns[9];
                    filter.CheckConGioiHanChoVayKhongTsbd = pageRequest.SearchColumns[10];
                    break;
                case "3":
                    filter.CheckLhdn = pageRequest.SearchColumns[5];
                    filter.CheckKhoanVayPheDuyet = pageRequest.SearchColumns[6];
                    filter.CheckThoiGianPheDuyet = pageRequest.SearchColumns[7];
                    filter.CheckKhChuaCoThe = pageRequest.SearchColumns[8];
                    filter.CheckConGioiHanChoVayKhongTsbd = pageRequest.SearchColumns[9];
                    break;
                case "4":
                    filter.CheckLhdn = pageRequest.SearchColumns[3];
                    filter.CheckKhoanVayPheDuyet = pageRequest.SearchColumns[4];
                    filter.CheckThoiGianPheDuyet = pageRequest.SearchColumns[5];
                    filter.CheckKhChuaCoThe = pageRequest.SearchColumns[6];
                    filter.CheckConGioiHanChoVayKhongTsbd = pageRequest.SearchColumns[7];
                    break;
                default:
                    break;
            }
            var customerRecords = RetrievePreApprovedCustomerCreditCardForLoan(pageRequest, filter, 3);
            var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<XSellingCreditCardForLoanRecord>() : customerRecords.Records;
            List<object[]> listCust = new List<object[]>();
            if (resultCustomer.Count > 0)
            {
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "4":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Cif,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.CheckLhdn,
                                                             x.CheckKhoanVayPheDuyet,
                                                             x.CheckThoiGianPheDuyet,
                                                             x.CheckKhChuaCoThe,
                                                             x.CheckConGioiHanChoVayKhongTsbd
                        }).ToList();
                        break;
                    case "3":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             string.IsNullOrEmpty(x.Dao) ? string.Empty:x.Dao,
                                                             string.IsNullOrEmpty(x.DaoName)? string.Empty: x.DaoName,
                                                             x.Cif,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.CheckLhdn,
                                                             x.CheckKhoanVayPheDuyet,
                                                             x.CheckThoiGianPheDuyet,
                                                             x.CheckKhChuaCoThe,
                                                             x.CheckConGioiHanChoVayKhongTsbd
                        }).ToList();
                        break;
                    case "2":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Branch,
                                                             string.IsNullOrEmpty(x.Dao) ? string.Empty:x.Dao,
                                                             string.IsNullOrEmpty(x.DaoName)? string.Empty: x.DaoName,
                                                             x.Cif,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.CheckLhdn,
                                                             x.CheckKhoanVayPheDuyet,
                                                             x.CheckThoiGianPheDuyet,
                                                             x.CheckKhChuaCoThe,
                                                             x.CheckConGioiHanChoVayKhongTsbd
                        }).ToList();
                        break;
                    case "1":
                        listCust = resultCustomer.Select(x => new object[]{
                                                             x.Zone,
                                                             x.Branch,
                                                             string.IsNullOrEmpty(x.Dao) ? string.Empty:x.Dao,
                                                             string.IsNullOrEmpty(x.DaoName)? string.Empty: x.DaoName,
                                                             x.Cif,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.CheckLhdn,
                                                             x.CheckKhoanVayPheDuyet,
                                                             x.CheckThoiGianPheDuyet,
                                                             x.CheckKhChuaCoThe,
                                                             x.CheckConGioiHanChoVayKhongTsbd
                        }).ToList();
                        break;
                    default:
                        break;
                }
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = listCust
                };
            }
            else
            {
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = 1,
                    sColumns = pageRequest.ColumnNames,
                    aaData = new List<object[]>()
                };
                pageResponse.aaData.Add(new object[] { customerRecords.ErrorMessage });
            }
            return Json(pageResponse);
        }
        [Authorize]
        [HttpPost]
        public ActionResult PreApprovedBilData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICrossSelling> serviceClient = new ServiceClient<ICrossSelling>())
            {
                FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        filter.CusOpenDate = pageRequest.SearchColumns[7];
                        filter.Balance = pageRequest.SearchColumns[8];
                        filter.Balance1 = pageRequest.SearchColumns[9];
                        break;
                    case "2":
                        filter.CusOpenDate = pageRequest.SearchColumns[6];
                        filter.Balance = pageRequest.SearchColumns[7];
                        filter.Balance1 = pageRequest.SearchColumns[8];
                        break;
                    case "3":
                        filter.CusOpenDate = pageRequest.SearchColumns[5];
                        filter.Balance = pageRequest.SearchColumns[6];
                        filter.Balance1 = pageRequest.SearchColumns[7];
                        break;
                    case "4":
                        filter.CusOpenDate = pageRequest.SearchColumns[3];
                        filter.Balance = pageRequest.SearchColumns[4];
                        filter.Balance1 = pageRequest.SearchColumns[5];
                        break;
                    default:
                        break;
                }
                RetrievePreApprovedCustBilRequest request = new RetrievePreApprovedCustBilRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.XsellingController.CustomerXSellingData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Lang = CurrentLang,
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                var customerRecords = new RetrievePreApprovedCustBilResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerPreApprovedBil(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<XSellingCustomerRecord>() : customerRecords.Records;
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "4":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.SumLimit,
                                                             x.LimitBilDuocCap,
                                                             x.DateAprove.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                        }).ToList()
                        };
                        break;
                    case "3":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.SumLimit,
                                                             x.LimitBilDuocCap,
                                                             x.DateAprove.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                        }).ToList()
                        };
                        break;
                    case "2":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Branch,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.SumLimit,
                                                             x.LimitBilDuocCap,
                                                             x.DateAprove.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                        }).ToList()
                        };
                        break;
                    case "1":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Zone,
                                                             x.Branch,
                                                             string.IsNullOrEmpty(x.Dao) ? string.Empty:x.Dao,
                                                             string.IsNullOrEmpty(x.DaoName)? string.Empty: x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.SumLimit,
                                                             x.LimitBilDuocCap,
                                                             x.DateAprove.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                        }).ToList()
                        };
                        break;
                    default:
                        break;
                }
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult PreApprovedWCData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICrossSelling> serviceClient = new ServiceClient<ICrossSelling>())
            {
                FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        filter.CusOpenDate = pageRequest.SearchColumns[7];
                        filter.Balance = pageRequest.SearchColumns[8];
                        break;
                    case "2":
                        filter.CusOpenDate = pageRequest.SearchColumns[6];
                        filter.Balance = pageRequest.SearchColumns[7];
                        break;
                    case "3":
                        filter.CusOpenDate = pageRequest.SearchColumns[5];
                        filter.Balance = pageRequest.SearchColumns[6];
                        break;
                    case "4":
                        filter.CusOpenDate = pageRequest.SearchColumns[3];
                        filter.Balance = pageRequest.SearchColumns[4];
                        break;
                    default:
                        break;
                }
                RetrievePreApprovedCustWCRequest request = new RetrievePreApprovedCustWCRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.XsellingController.CustomerXSellingData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Lang = CurrentLang,
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                var customerRecords = new RetrievePreApprovedCustWCResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerPreApprovedWC(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<XSellingCustomerRecord>() : customerRecords.Records;
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "4":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.SumLimit,
                                                             x.DateAprove.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.ExpiryDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                        }).ToList()
                        };
                        break;
                    case "3":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.SumLimit,
                                                             x.DateAprove.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.ExpiryDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                        }).ToList()
                        };
                        break;
                    case "2":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Branch,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.SumLimit,
                                                             x.DateAprove.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.ExpiryDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                        }).ToList()
                        };
                        break;
                    case "1":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Zone,
                                                             x.Branch,
                                                             string.IsNullOrEmpty(x.Dao) ? string.Empty:x.Dao,
                                                             string.IsNullOrEmpty(x.DaoName)? string.Empty: x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.SumLimit,
                                                             x.DateAprove.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.ExpiryDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                        }).ToList()
                        };
                        break;
                    default:
                        break;
                }
            }
            return Json(pageResponse);
        }
        #endregion

        #region Rate Limit
        [CustomActionFilter]
        [Authorize]
        public ActionResult ByRateLimit()
        {
            CrossSellingViewModel vm = new CrossSellingViewModel
            {
                CurrentUser = this.CurrentUserRecord
            };
            return View(vm);
        }

        [Authorize]
        [HttpPost]
        public ActionResult RateLimitData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICrossSelling> serviceClient = new ServiceClient<ICrossSelling>())
            {
                FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        filter.CusOpenDate = pageRequest.SearchColumns[7];
                        filter.Balance = pageRequest.SearchColumns[8];
                        filter.Balance1 = pageRequest.SearchColumns[9];
                        filter.Lift = pageRequest.SearchColumns[10];
                        break;
                    case "2":
                        filter.CusOpenDate = pageRequest.SearchColumns[6];
                        filter.Balance = pageRequest.SearchColumns[7];
                        filter.Balance1 = pageRequest.SearchColumns[8];
                        filter.Lift = pageRequest.SearchColumns[9];
                        break;
                    case "3":
                        filter.CusOpenDate = pageRequest.SearchColumns[5];
                        filter.Balance = pageRequest.SearchColumns[6];
                        filter.Balance1 = pageRequest.SearchColumns[7];
                        filter.Lift = pageRequest.SearchColumns[8];
                        break;
                    case "4":
                        filter.CusOpenDate = pageRequest.SearchColumns[3];
                        filter.Balance = pageRequest.SearchColumns[4];
                        filter.Balance1 = pageRequest.SearchColumns[5];
                        filter.Lift = pageRequest.SearchColumns[6];
                        break;
                    default:
                        break;
                }
                RetrieveCustomerByRateLimitRequest request = new RetrieveCustomerByRateLimitRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.XsellingController.CustomerXSellingData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Lang = CurrentLang,
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                var customerRecords = new RetrieveCustomerByRateLimitResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerByRateLimit(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<XSellingCustomerRecord>() : customerRecords.Records;
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "4":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateAprove.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.TotalLimit,
                                                             x.AvailLimit,
                                                             x.RateQd
                        }).ToList()
                        };
                        break;
                    case "3":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateAprove.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.TotalLimit,
                                                             x.AvailLimit,
                                                             x.RateQd
                        }).ToList()
                        };
                        break;
                    case "2":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Branch,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateAprove.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.TotalLimit,
                                                             x.AvailLimit,
                                                             x.RateQd
                        }).ToList()
                        };
                        break;
                    case "1":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Zone,
                                                             x.Branch,
                                                             string.IsNullOrEmpty(x.Dao) ? string.Empty:x.Dao,
                                                             string.IsNullOrEmpty(x.DaoName)? string.Empty: x.DaoName,
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateAprove.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.TotalLimit,
                                                             x.AvailLimit,
                                                             x.RateQd
                        }).ToList()
                        };
                        break;
                    default:
                        break;
                }
            }
            return Json(pageResponse);
        }
        #endregion
        #region Helper Method
        private RetrievePreApprovedCustCreditCardForLoanResponse RetrievePreApprovedCustomerCreditCardForLoan(DataTablesPageRequest pageRequest, FilterRecord filter, int type)
        {
            RetrievePreApprovedCustCreditCardForLoanResponse result = new RetrievePreApprovedCustCreditCardForLoanResponse();
            using (ServiceClient<ICrossSelling> serviceClient = new ServiceClient<ICrossSelling>())
            {
                RetrievePreApprovedCustCreditCardForLoanRequest request = new RetrievePreApprovedCustCreditCardForLoanRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.XsellingController.CustomerXSellingData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Lang = CurrentLang,
                    CurrentUser = CurrentUserRecord,
                    Filter = filter,
                    Type = type
                };
                result = serviceClient.Proxy.RetrieveCustomerPreApprovedCreditCardForLoan(request);
            }
            return result;
        }
        #endregion
    }
}
